package xxxx.com.utils;


import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * Druid连接池工具类
 * @author dullwolf
 *
 */
public class JDBCUtil {

    //定义成员变量
    private static DataSource dataSource;
    //静态代码块
    static {
        try {
            //加载配置文件
            Properties prop = new Properties();
            prop.load(JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
            //获取数据库连接池DataSource
            dataSource = DruidDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取dataSource
     */
    public static DataSource getDataSource(){
        return dataSource;
    }

    /**
     * 获取连接
     * @return 连接对象
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }


    /**
     * 关闭DQL（查询语句）资源
     * @param rs 释放结果集对象 ResultSet
     * @param stmt 释放执行SQL语句对象 Statement
     * @param conn 释放数据库连接对象 Connection
     */
    public static void close(ResultSet rs,Statement stmt, Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close(); //归还连接到连接池
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
